/* 
 * Advanced Foundation Classes
 * Copyright (C) 2000/2004  Fabio Rotondo - Andrea Galimberti
 *  
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *  
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *  
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 *
 */
#include <afc/easygui/easygui.h>
#include <afc/easygui/widgets.h>

#define EGUI_ENTRY_GET_VALUE2(cnt,dc,name,var)	w = afc_easygui_context_get_widget ( cnt, name ); \
						afc_dynamic_class_execute ( dc, "get_value", AFC_EASYGUI_ARG_END ); \
						var = dc->result;

int clist_activate ( DynamicClass * dc, EasyGUIContext * context, void * info )
{
	printf ( "Activate!\n" );
	return ( AFC_ERR_NO_ERROR );
}

int clist_changed ( DynamicClass * dc, EasyGUIContext * context, short selected, void * info )
{
	printf ( "Row Selected: %d\n", selected );

	return ( AFC_ERR_NO_ERROR );
}

int btn_add ( DynamicClass * dc, EasyGUIContext * context )
{
	char * name, * surname, * email, * age;
	DynamicClass * w;

	EGUI_ENTRY_GET_VALUE2 ( context, w, "entry_name", name );
	EGUI_ENTRY_GET_VALUE2 ( context, w, "entry_surname", surname );
	EGUI_ENTRY_GET_VALUE2 ( context, w, "entry_email", email );
	EGUI_ENTRY_GET_VALUE2 ( context, w, "entry_age", age );
	
	afc_easygui_widget_execute ( context, "clist", "set_back_color", 255, 255, 255, AFC_EASYGUI_ARG_END );
	afc_easygui_widget_execute ( context, "clist", "add", name, surname, email, age, NULL, AFC_EASYGUI_ARG_END );
	return ( AFC_ERR_NO_ERROR );
}

int btn_del ( DynamicClass * dc, EasyGUIContext * context )
{
	printf ( "DEL\n" );
	return ( AFC_ERR_NO_ERROR );
}

int main ( int argc, char * argv[] )
{
	AFC * afc = afc_new (); 
	EasyGUI * easygui = afc_easygui_new ( afc );
	EasyGUIContext * context;
	int afc_res = 0;

	if ( easygui == NULL ) 
	{
	  fprintf ( stderr, "Init of class EasyGUI failed.\n" );
	  return ( 1 );
	}

	afc_easygui_init ( easygui );
	context = afc_easygui_add_context ( easygui, NULL );

	afc_easygui_context_set_gui ( context, 
		"(window TITLE=\"EasyGUI: CList\" MAINWIN "
		"  (rows "
		"    (clist NAME=\"clist\" EXPAND FILL COLUMNS=\"Name|Surname|E-Mail|Age\" SELMODE=MULTI ALIGNMENT=\"LEFT|LEFT|LEFT|RIGHT\" "
		"           ACTIVATED=\"clist_activate\" CHANGED=\"clist_changed\" DNDDEST=\"clist_changed\" ) "
		"	(frame TITLE=\"CList Controls\" SHADOW=\"IN\" "
		"	(rows "
		"			(cols "
		"				(label TEXT=\"Name:\") "
		"				(entry NAME=\"entry_name\") "
		"				(label TEXT=\"Surname:\") "
		"				(entry NAME=\"entry_surname\") "
		"			)"
		"			(cols "
		"				(label TEXT=\"E-Mail:\") "
		"				(entry NAME=\"entry_email\") "
		"				(label TEXT=\"Age:\") "
		"				(entry NAME=\"entry_age\") "
		"			)"
		"		)"
		"	)"
		"	(separator)"
		"	(eqcols "
		"		(button TEXT=\"Add\" ICON=\"@add\" ACTION=\"btn_add\") "
		"		(separator) "
		"		(button TEXT=\"Del\" ICON=\"@remove\" ACTION=\"btn_del\") "
		"	) "
		"  ) "
		") "
	);

	if ( ( afc_res = afc_easygui_context_show ( context ) ) != AFC_ERR_NO_ERROR ) 
	{
		fprintf ( stderr, "res: %x\n", afc_res );
	} else 
		afc_easygui_main ( easygui );

	afc_easygui_delete ( easygui );
	afc_delete ( afc );

	return ( 0 ); 
}
